﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using System.Security.Cryptography;
using System.Text;

public partial class Management_Users_Admin : PageBase
{
    private XDocument _usersXML;
    public XDocument UsersXML
    {
        get
        {
            if (this._usersXML == null)
                this._usersXML = XDocument.Load(Server.MapPath("~/App_Data/xmldata/users.xml"));
            return this._usersXML;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void lbtnSave_Click(object sender, EventArgs e)
    {
        try
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] hashData = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(this.txtPWD.Text.Trim()));
            StringBuilder returnValue = new StringBuilder();
            for (int i = 0; i < hashData.Length; i++)
            {
                returnValue.Append(hashData[i].ToString());
            }
            byte[] hashDataNews = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(this.txtNewsPWD.Text.Trim()));
            StringBuilder returnValueNews = new StringBuilder();
            for (int i = 0; i < hashDataNews.Length; i++)
            {
                returnValueNews.Append(hashDataNews[i].ToString());
            }
            byte[] hashDataProduct = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(this.txtProductPWD.Text.Trim()));
            StringBuilder returnValueProduct = new StringBuilder();
            for (int i = 0; i < hashDataProduct.Length; i++)
            {
                returnValueProduct.Append(hashDataProduct[i].ToString());
            }
            if (this.txtPWD.Text.Trim().Length > 0)
            {
                this.UsersXML.Descendants("User").SingleOrDefault(u => u.Attribute("type").Value == "admin").Element("Password").SetValue(returnValue.ToString());
            }
            if (this.txtNewsPWD.Text.Trim().Length > 0)
            {
                this.UsersXML.Descendants("User").SingleOrDefault(u => u.Attribute("type").Value == "news").Element("Password").SetValue(returnValueNews.ToString());
            }
            if (this.txtProductPWD.Text.Trim().Length > 0)
            {
                this.UsersXML.Descendants("User").SingleOrDefault(u => u.Attribute("type").Value == "product").Element("Password").SetValue(returnValueProduct.ToString());
            }
            this.UsersXML.Save(Server.MapPath("~/App_Data/xmldata/users.xml"));
            this.Session[Utils.SESSIONUSER] = null;
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Key1", "javascript:" + string.Format("alert('{0}');window.location.href='../Default.aspx';", "操作成功!"), true);
        }
        catch
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Key2", "javascript:" + string.Format("alert('{0}');window.location.reload();", "保存时发生错误!请重试..."), true);
        }

    }
}